package eproject.mm.com.projectsupervision.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import java.util.List;
import java.util.Map;

import eproject.mm.com.projectsupervision.R;
import eproject.mm.com.projectsupervision.adapter.DataAdapter;
import eproject.mm.com.projectsupervision.util.HttpUtil;
import eproject.mm.com.projectsupervision.widget.IconFontTextview;

public class MessageContentActivity extends Activity {
    TextView tv_content,tv_title,tv_time;
    LoadingDialog loadingDialog = null;

    String id;
    IconFontTextview iftv_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_content);

        Bundle bundle = getIntent().getExtras();
        id = bundle.getString("id","");

        initView();
        initEvent();
        loadData();

        iftv_back = (IconFontTextview)findViewById(R.id.iftv_back);
        iftv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    public void initView(){
        tv_content = (TextView)findViewById(R.id.tv_content);
        tv_title = (TextView)findViewById(R.id.tv_title);
        tv_time = (TextView)findViewById(R.id.tv_time);
    }
    private void initEvent(){

    }
    public void back(View view) {
        finish();
    }
    public void loadData(){
        loadingDialog = new LoadingDialog(this);
        loadingDialog.setLoadingText("加载中...").show();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String str = HttpUtil.get("/getPushMsg?id=" + id);

                    handler.sendMessage(handler.obtainMessage(1, str));
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(MessageContentActivity.this,e.toString(),Toast.LENGTH_LONG);
                    handler.sendMessage(handler.obtainMessage(2, null));
                }
            }
        }).start();

        tv_content.setText(Html.fromHtml(""));
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    String str = (String)msg.obj;
                    Gson gson=new Gson();
                    Map resultMap = gson.fromJson(str, Map.class);
                    if (resultMap != null && (boolean) resultMap.get("success")) {
                        List list = (List)resultMap.get("rows");
                        if(list!=null && list.size()>0){
                            Map msgMap = (Map)list.get(0);
                            String content = (String)msgMap.get("content");
                            if(content==null)content="";
                            tv_content.setText(Html.fromHtml(content));
                            tv_time.setText((String)msgMap.get("createtime"));
                            tv_title.setText((String)msgMap.get("title"));
                        }
                        loadingDialog.close();
                    }
                    break;
                case 2:
                    loadingDialog.close();
                    break;
                default:
                    super.handleMessage(msg);
                    break;
            }
        }
    };
}
